package com.k.liuzh.extraffic.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.k.liuzh.extraffic.Database.ChongZhiBean;
import com.k.liuzh.extraffic.Database.ChongZhiDao;
import com.k.liuzh.extraffic.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

public class ZhangHu_Fragment extends Fragment {
    @Nullable
    private TextView textViewYuE;
    private EditText editTextChongZhi;
    private Button btnChaXun;
    private Button btnChongZhi;
    private Spinner spinner;
    private RequestQueue requestQueue;
    private Bundle bundle;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_zhanghu,container,false);
        textViewYuE = view.findViewById(R.id.textView_zhanghu_yue);
        editTextChongZhi = view.findViewById(R.id.editText_zhanghu_chongzhi);
        spinner = view.findViewById(R.id.spn_zhanghu_chehao);
        btnChaXun = view.findViewById(R.id.btn_zhanghu_chaxun);
        btnChongZhi = view.findViewById(R.id.btn_zhanghu_chongzhi);
        chaXun();
        btnChaXun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chaXun();
            }
        });

        btnChongZhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String value = editTextChongZhi.getText().toString();
                if("0".equals(value) || "".equals(value)){
                    Toast.makeText(getContext(),"请输入充值金额",Toast.LENGTH_SHORT).show();
                }else {
                    chongZhi();
                }
            }
        });
        return view;
    }

    private void chaXun() {
        bundle = this.getArguments();
        String userName = bundle.getString("userName");
        String carId = spinner.getSelectedItem().toString();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("CarId",carId);
            jsonObject.put("UserName",userName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url = "http://47.106.153.72:8081/transportservice/action/GetCarAccountBalance.do";
        requestQueue = Volley.newRequestQueue(getContext());
        final JsonObjectRequest js = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                String result = jsonObject.optString("RESULT");
                if("S".equals(result)){
                    final String yuE = jsonObject.optString("Balance");
                    textViewYuE.setText(yuE);

                }else {
                    Toast.makeText(getContext(),"查询失败",Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getContext(),"网络错误",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(js);
    }
    private void chongZhi(){
        bundle = this.getArguments();
        String userName = bundle.getString("userName");
        String money = editTextChongZhi.getText().toString();
        String carId = spinner.getSelectedItem().toString();
        requestQueue = Volley.newRequestQueue(getContext());
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("CarId",carId);
            jsonObject.put("Money",money);
            jsonObject.put("UserName",userName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url = "http://47.106.153.72:8081/transportservice/action/SetCarAccountRecharge.do";
        final JsonObjectRequest js = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                String result = jsonObject.optString("RESULT");
                if("S".equals(result)){
                    Toast.makeText(getContext(),"充值成功",Toast.LENGTH_SHORT).show();
                    chaXun();
                    sql();
                }else {
                    Toast.makeText(getContext(),"充值失败",Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getContext(),"网络错误",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(js);
    }

    private void sql() {
        String carid = spinner.getSelectedItem().toString();
        int carId = Integer.parseInt(carid);
        bundle = this.getArguments();
        String userName = bundle.getString("userName");
        int jinE = Integer.parseInt(editTextChongZhi.getText().toString());
        ChongZhiBean chongZhiBean = new ChongZhiBean(carId,jinE,userName,new Date());
        new ChongZhiDao(getContext()).add(chongZhiBean);
    }

}
